const Nav = require("../common/nav/index.js")
Page({
    data: {
        list: [
            { name: "我收藏的" },
            { name: "收藏我的" },
        ]
    },
    onLoad: function (options) {
        new Nav({
            list: this.data.list.map(o => {
                return { name: o.name }
            })
        }).on("change", current => {
            this.setData({ current })
            const { list = [] } = this.data.list[current]
            !list.length && this.loadData()
        })
    },
    loadData() {
        const { current } = this.data
        let { page = 1, total = 2, loading = false, list = [] } = this.data.list[current]
        if (page > total || loading) {
            return
        }
        this.setData({
            [`list[${current}].loading`]: true
        })
        getApp().http.get("city/member-card/keep-list", {
            data: {
                page,
                type: current == 0 ? 1 : 2
            }
        }).then(res => {
            page++
            total = res.data.total
            list.push(...res.data.items)
            loading = false
            this.setData({
                [`list[${current}]`]: Object.assign(this.data.list[current], {
                    page, total, list, loading
                })
            })
            wx.stopPullDownRefresh()
        })
    },
    onReachBottom() {
        this.loadData()
    },
    onPullDownRefresh() {
        this.setData({
            [`list[${this.data.current}]`]: {}
        })
        this.loadData()
    }
})